package com.nasarius.bank.scorecard.dao.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import org.springframework.stereotype.Repository;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.nasarius.bank.scorecard.dao.SurveyAnswersDAO;
import com.nasarius.bank.scorecard.domain.SurveyAnswers;
import com.nasarius.bank.scorecard.domain.SurveyAnswersExample;
@Repository
public class SurveyAnswersDAOImpl extends SqlMapClientDaoSupport implements SurveyAnswersDAO {

    /**
     * This method was generated by Apache iBATIS ibator.
     * This method corresponds to the database table survey_answers
     *
     * @ibatorgenerated Sun Mar 13 00:13:58 IST 2011
     */
	@Autowired
    public SurveyAnswersDAOImpl(/*@Qualifier("sqlMapClient")*/SqlMapClient sqlMapClient) {
        super();
        setSqlMapClient(sqlMapClient);
    }

    /**
     * This method was generated by Apache iBATIS ibator.
     * This method corresponds to the database table survey_answers
     *
     * @ibatorgenerated Sun Mar 13 00:13:58 IST 2011
     */
    public int countSurveyAnswersByExample(SurveyAnswersExample example) {
        Integer count = (Integer)  getSqlMapClientTemplate().queryForObject("survey_answers.ibatorgenerated_countByExample", example);
        return count;
    }

    /**
     * This method was generated by Apache iBATIS ibator.
     * This method corresponds to the database table survey_answers
     *
     * @ibatorgenerated Sun Mar 13 00:13:58 IST 2011
     */
    public int deleteSurveyAnswersByExample(SurveyAnswersExample example) {
        int rows = getSqlMapClientTemplate().delete("survey_answers.ibatorgenerated_deleteByExample", example);
        return rows;
    }

    /**
     * This method was generated by Apache iBATIS ibator.
     * This method corresponds to the database table survey_answers
     *
     * @ibatorgenerated Sun Mar 13 00:13:58 IST 2011
     */
    public void insertSurveyAnswers(SurveyAnswers record) {
        getSqlMapClientTemplate().insert("survey_answers.ibatorgenerated_insert", record);
    }

    /**
     * This method was generated by Apache iBATIS ibator.
     * This method corresponds to the database table survey_answers
     *
     * @ibatorgenerated Sun Mar 13 00:13:58 IST 2011
     */
    public void insertSurveyAnswersSelective(SurveyAnswers record) {
        getSqlMapClientTemplate().insert("survey_answers.ibatorgenerated_insertSelective", record);
    }

    /**
     * This method was generated by Apache iBATIS ibator.
     * This method corresponds to the database table survey_answers
     *
     * @ibatorgenerated Sun Mar 13 00:13:58 IST 2011
     */
    @SuppressWarnings("unchecked")
    public List<SurveyAnswers> selectSurveyAnswersByExample(SurveyAnswersExample example) {
        List<SurveyAnswers> list = getSqlMapClientTemplate().queryForList("survey_answers.ibatorgenerated_selectByExample", example);
        return list;
    }

    /**
     * This method was generated by Apache iBATIS ibator.
     * This method corresponds to the database table survey_answers
     *
     * @ibatorgenerated Sun Mar 13 00:13:58 IST 2011
     */
    public int updateSurveyAnswersByExampleSelective(SurveyAnswers record, SurveyAnswersExample example) {
        UpdateByExampleParms parms = new UpdateByExampleParms(record, example);
        int rows = getSqlMapClientTemplate().update("survey_answers.ibatorgenerated_updateByExampleSelective", parms);
        return rows;
    }

    /**
     * This method was generated by Apache iBATIS ibator.
     * This method corresponds to the database table survey_answers
     *
     * @ibatorgenerated Sun Mar 13 00:13:58 IST 2011
     */
    public int updateSurveyAnswersByExample(SurveyAnswers record, SurveyAnswersExample example) {
        UpdateByExampleParms parms = new UpdateByExampleParms(record, example);
        int rows = getSqlMapClientTemplate().update("survey_answers.ibatorgenerated_updateByExample", parms);
        return rows;
    }

    /**
     * This class was generated by Apache iBATIS ibator.
     * This class corresponds to the database table survey_answers
     *
     * @ibatorgenerated Sun Mar 13 00:13:58 IST 2011
     */
    private static class UpdateByExampleParms extends SurveyAnswersExample {
        private Object record;

        public UpdateByExampleParms(Object record, SurveyAnswersExample example) {
            super(example);
            this.record = record;
        }

        @SuppressWarnings("unused")
		public Object getRecord() {
            return record;
        }
    }
    /**
     * This method was generated by Apache iBATIS ibator.
     * This method corresponds to the database table survey_answers
     *
     * @ibatorgenerated Sun Mar 13 00:13:58 IST 2011
     */
    @SuppressWarnings("unchecked")
    public List<SurveyAnswers> selectSurveyAnswersBySurveyID(String username, Integer surveyId, Integer topicID) {
		Map<String,Object> criteria= new HashMap<String,Object>();
		criteria.put("username",username); 
		criteria.put("surveyId",surveyId); 
		criteria.put("topicID", topicID);
        List<SurveyAnswers> list = getSqlMapClientTemplate().queryForList("survey_answers.ibatorgenerated_selectBySurveyID", criteria);
        return list;
    }
    /**
     * This method was generated by Apache iBATIS ibator.
     * This method corresponds to the database table survey_answers
     *
     * @ibatorgenerated Sun Mar 13 00:13:58 IST 2011
     */
    @SuppressWarnings("unchecked")
    public List<SurveyAnswers> selectSurveyAnswersBySurveyQuestionID(String surveyQuestionId, String username) {
		Map<String,Object> criteria= new HashMap<String,Object>();
		criteria.put("surveyquestionid",surveyQuestionId); 
		criteria.put("username",username);     	
        List<SurveyAnswers> list = getSqlMapClientTemplate().queryForList("survey_answers.ibatorgenerated_selectBySurveyQuestionID", criteria);
        return list;
    }    
    /**
     * This method was generated by Apache iBATIS ibator.
     * This method corresponds to the database table bank_details
     *
     * @ibatorgenerated Sun Mar 13 00:13:58 IST 2011
     */
    public int updateByQuestionID(SurveyAnswers record) {
        int rows = getSqlMapClientTemplate().update("survey_answers.ibatorgenerated_updateByQuestionID", record);
        return rows;
    }
	@SuppressWarnings("unchecked")
	@Override
	public String surveyAnswersByFactor(String bankcode, String factorId,
			Date fromDt, Date toDt) {
		SurveyAnswers key = new SurveyAnswers();
		key.setBankcode(bankcode);
		key.setFactorid(Integer.valueOf(factorId));
		key.setStartdate(fromDt);
		key.setEnddate(toDt);
		String answerCode="0";
		List<SurveyAnswers> list =(List<SurveyAnswers>) getSqlMapClientTemplate().queryForList("survey_answers.surveyAnswersByFactor", key);
		for(SurveyAnswers surveyAnswers:list){
			if (surveyAnswers.getAnswercode() != null){
				answerCode=surveyAnswers.getAnswercode();
			}				
		}
	    return answerCode;
	}
	@SuppressWarnings("unchecked")
	@Override
	public String surveyAnswersByBankCode(String bankCode,
			Date fromDt, Date toDt) {
		SurveyAnswers key = new SurveyAnswers();
		key.setBankcode(bankCode);
		key.setStartdate(fromDt);
		key.setEnddate(toDt);
		List<SurveyAnswers> answerDescList = (List<SurveyAnswers>) getSqlMapClientTemplate().queryForList("survey_answers.surveyAnswerDescByFactor", key);
		String answerDesc = formAnswerDesc(answerDescList);
	    return answerDesc;
	}	

	private String formAnswerDesc(List<SurveyAnswers> answerDescList) {
		String answerDesc = "";
		for(SurveyAnswers surveyAnswers:answerDescList){
			if(StringUtils.isNotBlank(surveyAnswers.getAnswerdescription())){
				answerDesc=answerDesc+surveyAnswers.getAnswerdescription()+"<br/>";
			}
		}		
		return answerDesc;
	}    
	
	@SuppressWarnings("unchecked")
	@Override
	public List<SurveyAnswers> surveyUserNameByFactor(Integer surveyId, List<String> factorIdList) {
		Map<String,Object> criteria= new HashMap<String,Object>(); 
		criteria.put("surveyId",surveyId);
		criteria.put("factorIdList",factorIdList);
		List<SurveyAnswers> list =(List<SurveyAnswers>) getSqlMapClientTemplate().queryForList("survey_answers.surveyUserNameByFactor", criteria);
	    return list;
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<SurveyAnswers> surveyQuestionsByFactor(Integer surveyId, List<String> factorIdList) {
		Map<String,Object> criteria= new HashMap<String,Object>(); 
		criteria.put("surveyId",surveyId);
		criteria.put("factorIdList",factorIdList);
		List<SurveyAnswers> list =(List<SurveyAnswers>) getSqlMapClientTemplate().queryForList("survey_answers.surveyQuestionsByFactor", criteria);
	    return list;
	}	
}